#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class VoteRoleProvider : CY.GFive.Core.Providers.DALProvider.IVoteRoleProvider
	{
		#region Sql
		private static readonly string SqlSelectVoteRole = "SELECT [Id], [DID],[VoteID],[RoleID] FROM [VoteRole] WHERE  [Id]=@Id";

		private static readonly string SqlInsertVoteRole = "INSERT INTO [VoteRole]([DID],[VoteID],[RoleID]) values(@DID,@VoteID,@RoleID);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateVoteRole = "UPDATE [VoteRole] SET [DID] = @DID,[VoteID] = @VoteID,[RoleID] = @RoleID WHERE  [Id]=@Id";

		private static readonly string SqlDeleteVoteRole = "DELETE [VoteRole] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllVoteRole = "SELECT [Id], [DID],[VoteID],[RoleID] FROM [VoteRole]";
		#endregion

		#region IVoteRoleProvider Members
		public Core.Business.VoteRole Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectVoteRole);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.VoteRole voteRole = new Core.Business.VoteRole();

				if (!reader.IsDBNull(0)) voteRole.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) voteRole.DID = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) voteRole.VoteID = reader.GetInt32(2);
				if (!reader.IsDBNull(3)) voteRole.RoleID = reader.GetInt32(3);

				return voteRole;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.VoteRole voteRole)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@DID", SqlDbType.Int, voteRole.DID);
			sql.AddParameter("@VoteID", SqlDbType.Int, voteRole.VoteID);
			sql.AddParameter("@RoleID", SqlDbType.Int, voteRole.RoleID);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertVoteRole);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) voteRole.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.VoteRole voteRole)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@DID", SqlDbType.Int, voteRole.DID);
			sql.AddParameter("@VoteID", SqlDbType.Int, voteRole.VoteID);
			sql.AddParameter("@RoleID", SqlDbType.Int, voteRole.RoleID);
			sql.AddParameter("@Id", SqlDbType.Int, voteRole.Id);
			sql.ExecuteSql(SqlUpdateVoteRole);
		}

		public void Delete(Core.Business.VoteRole voteRole)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, voteRole.Id);
			sql.ExecuteSql(SqlDeleteVoteRole);
		}

		public IList<Core.Business.VoteRole> GetAllVoteRole()
		{
			IList<Core.Business.VoteRole> voteRolelist = new List<Core.Business.VoteRole>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllVoteRole);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.VoteRole voteRole = new Core.Business.VoteRole();

					if (!reader.IsDBNull(0)) voteRole.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) voteRole.DID = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) voteRole.VoteID = reader.GetInt32(2);
					if (!reader.IsDBNull(3)) voteRole.RoleID = reader.GetInt32(3);

					voteRole.MarkOld();
					voteRolelist.Add(voteRole);
				}
				reader.Close();
			}
			return voteRolelist;
		}
		#endregion
	}
}
